//
//  systemMessageVC.swift
//  DingDongZP
//
//  Created by Zhongnan on 16/4/11.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class systemMessageVC: UITableViewController {

    var messageArr = [NSDictionary]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tableview = UITableView.init(frame: self.view.frame, style: UITableViewStyle.Grouped)
        
        tableview.translatesAutoresizingMaskIntoConstraints = false
        tableview.contentInset = UIEdgeInsetsMake(85, 0, 0, 0)
        tableview.separatorStyle = UITableViewCellSeparatorStyle.None
        
        let marView = UIView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, 15))
        marView.backgroundColor = LDGolbalColor.BgGoundGray
        tableview.tableHeaderView = marView

        tableview.delegate = self
        self.tableView = tableview
        view.backgroundColor = LDGolbalColor.BgGoundGray
        requestNewMessage()
        
    }
    
    func requestNewMessage() {
        
        LDUserInfoNet.sharedInstance.querySystemMessage({[weak self] (dict) in
            
            self?.messageArr = dict
            self?.tableView.reloadData()
            
        }) { (error) in
            
            LDShowHUB.shareInstance.showBarStatus(error as? String ?? "失败")
        }
    }
    
}
extension systemMessageVC {
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return messageArr.count
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCellWithIdentifier("cells") as? systemMessageCell
        if cell == nil {
            cell = systemMessageCell.init(style: .Default, reuseIdentifier: "cells")
        }
        
        cell?.models = systemMessageCellModel.yy_modelWithDictionary(messageArr[indexPath.section] as [NSObject : AnyObject])
        
        cell?.tapCellAction({
            
            let vc = LDBaseWebViewController()
            vc.reqUrl = cell?.models?.h5 ?? ""
            
            let nav = UIApplication.sharedApplication().keyWindow?.rootViewController?.childViewControllers[0] as! LDRootNavVC
            nav.pushViewController(vc, animated: true)
        })
        
        
        return cell!
    }
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 320
    }
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let model = systemMessageCellModel.yy_modelWithDictionary(messageArr[section] as [NSObject : AnyObject])
        
        let views = systemMessTimeHeadView.init(frame: CGRectMake(0, 0, LDFrame.ScreenWidth, 25))
        views.timeLab?.text = model?.datetime//"2015月8月 18:00"
        return views
    }
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 25
    }
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
}
